/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;


import javax.swing.JPanel;
import javax.swing.table.DefaultTableModel;
import java.awt.Component;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import java.lang.String;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.AbstractTableModel;
import dao.LoaiNguoiDungDAO;
import dao.NguoiDungDAO;
import pojo.LoaiNguoiDung;
import pojo.NguoiDung;

import GUI.MainGUI;


/**
 *
 * @author BuiThiKieu
 */
public class DanhSachKhachHangGUI extends javax.swing.JPanel {
    private DefaultTableModel tableModel;
    private List<NguoiDung> nguoiDungList;
     private NguoiDung nguoiDung;
    private static org.apache.log4j.Logger logger=org.apache.log4j.Logger.getLogger(DanhSachKhachHangGUI.class);

   
    /**
     * Creates new form KhachHangGUI
     */
    public DanhSachKhachHangGUI() throws Exception {
        initComponents();
        AutoLoad();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        dtNgaySinh = new org.jdesktop.swingx.JXDatePicker();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        txtID = new javax.swing.JTextField();
        txtTenKhachHang = new javax.swing.JTextField();
        txtCMND = new javax.swing.JTextField();
        txtDienThoai = new javax.swing.JTextField();
        txtDiaChi = new javax.swing.JTextField();
        btnThem = new javax.swing.JButton();
        btnCapNhat = new javax.swing.JButton();
        btnXoa = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbDanhSachKhachHang = new javax.swing.JTable();

        setLayout(null);

        jPanel1.setBackground(new java.awt.Color(255, 255, 204));

        jLabel1.setText("ID");

        jLabel2.setText("Tên khách hàng");

        jLabel3.setText("CMNN");

        jLabel5.setText("Địa chỉ");

        jLabel6.setText("Ngày sinh");

        txtID.setEditable(false);
        txtID.setText("0");

        btnThem.setText("Thêm");
        btnThem.setFocusCycleRoot(true);
        btnThem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThemActionPerformed(evt);
            }
        });

        btnCapNhat.setText("Cập nhật");
        btnCapNhat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCapNhatActionPerformed(evt);
            }
        });

        btnXoa.setText("Xóa");
        btnXoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnXoaActionPerformed(evt);
            }
        });

        jLabel4.setText("Điện thoại");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(42, 42, 42)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel3)
                    .addComponent(jLabel5)
                    .addComponent(jLabel4))
                .addGap(36, 36, 36)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtCMND, javax.swing.GroupLayout.DEFAULT_SIZE, 98, Short.MAX_VALUE)
                            .addComponent(txtID)
                            .addComponent(txtDienThoai))
                        .addGap(33, 33, 33)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(41, 41, 41)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(dtNgaySinh, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtTenKhachHang, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(txtDiaChi, javax.swing.GroupLayout.PREFERRED_SIZE, 290, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(83, Short.MAX_VALUE))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(162, Short.MAX_VALUE)
                .addComponent(btnThem)
                .addGap(37, 37, 37)
                .addComponent(btnCapNhat)
                .addGap(38, 38, 38)
                .addComponent(btnXoa, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(183, 183, 183))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(17, 17, 17)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtTenKhachHang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtCMND, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(dtNgaySinh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel5)
                        .addGap(49, 49, 49))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(txtDienThoai, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txtDiaChi, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(40, 40, 40)))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnThem)
                    .addComponent(btnCapNhat)
                    .addComponent(btnXoa))
                .addGap(27, 27, 27))
        );

        add(jPanel1);
        jPanel1.setBounds(90, 20, 630, 230);

        tbDanhSachKhachHang.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        tbDanhSachKhachHang.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        tbDanhSachKhachHang.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbDanhSachKhachHangMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tbDanhSachKhachHang);
        tbDanhSachKhachHang.getAccessibleContext().setAccessibleDescription("");

        add(jScrollPane1);
        jScrollPane1.setBounds(10, 267, 820, 211);
    }// </editor-fold>//GEN-END:initComponents

    private void AutoLoad() throws Exception {
        this.tableModel = new DefaultTableModel();
         // Tạo các cột
        this.tableModel.addColumn("ID");
        this.tableModel.addColumn("Tên khách hàng");
        this.tableModel.addColumn("CMNN");
        this.tableModel.addColumn("Ngày sinh");
        this.tableModel.addColumn("Điện thoại");
        this.tableModel.addColumn("Địa chỉ");
        
        /*
         * Lấy danh sách người dùng
         */
        //List<NguoiDung> ds = NguoiDungDAO.LayDanhSachNguoiDung();
         nguoiDungList = NguoiDungDAO.LayDanhSachNguoiDung();
        for(int i = 0; i < nguoiDungList.size(); i++){
              Vector vt = new Vector();
              vt.add(nguoiDungList.get(i).getId());
              vt.add(nguoiDungList.get(i).getTenNguoiDung());
              vt.add(nguoiDungList.get(i).getCmnd());
              vt.add(nguoiDungList.get(i).getNgaySinh());
              vt.add(nguoiDungList.get(i).getDienThoai());
              vt.add(nguoiDungList.get(i).getDiaChi());
              this.tableModel.addRow(vt);
          }
        //Gán Model
        this.tbDanhSachKhachHang.setModel(tableModel);
    }
    
     private int CheckBeforeAdd(){
        int dem = 0;
        if(txtTenKhachHang.getText().trim().isEmpty()){
            dem++;
        }
        if((txtCMND.getText().trim().isEmpty()) )
        {
            dem++;
        }
        else{
            int dong = tbDanhSachKhachHang.getRowCount();
            for(int i = 0; i < dong; i++){
                if(tbDanhSachKhachHang.getValueAt(i, 2).toString() == txtCMND.getText().toString()){
                    dem++;
                }
            }
        }
        return dem;
    }
     
    private void btnThemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThemActionPerformed
        // TODO add your handling code here:
        if(CheckBeforeAdd() > 0){
            JOptionPane.showMessageDialog(this, "Vui lòng kiểm tra lại thông tin");
        }
        else{
            int dong = tbDanhSachKhachHang.getRowCount();
            for(int i = 0; i < dong; i++){
                int cmndTable = Integer.parseInt(tbDanhSachKhachHang.getValueAt(i, 2).toString());
                int cmndText = Integer.parseInt(txtCMND.getText().toString());
                if( cmndTable == cmndText){
                    JOptionPane.showMessageDialog(this, "Khách hàng đã có rồi");
                    return;
                }
            }
            NguoiDung khachHang = new NguoiDung();

            int id = Integer.parseInt(txtID.getText().toString());
            khachHang.setId(id);
            khachHang.setTenNguoiDung(txtTenKhachHang.getText().toString());
            khachHang.setCmnd(txtCMND.getText().toString());
            khachHang.setNgaySinh(dtNgaySinh.getDate());
            khachHang.setDienThoai(txtDienThoai.getText().toString());
            khachHang.setDiaChi(txtDiaChi.getText().toString());
            khachHang.setIsDelete(false);
            

            boolean kq = NguoiDungDAO.ThemKhachHang(khachHang);
            if(kq == true){
                try {
                    AutoLoad();
                    JOptionPane.showMessageDialog(this, "Thêm thành công");
                } catch (Exception ex) {
                    Logger.getLogger(DanhSachKhachHangGUI.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            else{
                JOptionPane.showMessageDialog(this, "Thêm thất bại");
            }
        }
    }//GEN-LAST:event_btnThemActionPerformed

    private void btnXoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnXoaActionPerformed
        // TODO add your handling code here:
       /* int dong = tbDanhSachKhachHang.getSelectedRow();
        if(dong >= 0){
            int id = Integer.parseInt(txtID.getText().toString());
            boolean kq = NguoiDungDAO.XoaKhachHang(id);
            if(kq == true){
                try {
                    AutoLoad();
                    JOptionPane.showMessageDialog(this, "Xóa thành công");
                } catch (Exception ex) {
                    Logger.getLogger(DanhSachKhachHangGUI.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            else
                JOptionPane.showMessageDialog(this, "Xóa thất bại");
        }
        else{
            JOptionPane.showMessageDialog(this, "Vui lòng chọn dữ liệu trên lưới để xóa");
        }
        * 
        */
        int dong = tbDanhSachKhachHang.getSelectedRow();
        if(dong < 0){
            JOptionPane.showMessageDialog(this, "Vui lòng chọn dữ liệu trên lưới để xóa");
        }
        else{
            int id = Integer.parseInt(txtID.getText().toString());
            NguoiDung nd = NguoiDungDAO.LayKhachHangByID(id);
            
            if(nd != null){
               /* nd.setCmnd(txtCMND.getText().toString());
                nd.setDienThoai(txtDienThoai.getText().toString());
                nd.setDiaChi(txtDiaChi.getText().toString());
                nd.setTenNguoiDung(txtTenKhachHang.getText().toString());
                nd.setNgaySinh(dtNgaySinh.getDate());*/
                nd.setIsDelete(true);
                boolean kq = NguoiDungDAO.CapNhatKhachHang(nd);
                if(kq == true){
                    try {
                        AutoLoad();
                        JOptionPane.showMessageDialog(this, "Xóa thành công");
                    } catch (Exception ex) {
                        Logger.getLogger(DanhSachKhachHangGUI.class.getName()).log(Level.SEVERE, null, ex);
                    }
                   
                }
                else{
                    JOptionPane.showMessageDialog(this, "Xóa thất bại");
                }
            }
        }
        return;
    }//GEN-LAST:event_btnXoaActionPerformed

    private void tbDanhSachKhachHangMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbDanhSachKhachHangMouseClicked
        // TODO add your handling code here:
        int dong = tbDanhSachKhachHang.getSelectedRow();
        if(dong >= 0){
            //txtID.setText(tbDanhSachKhachHang.get
            //txtTenKhachHang.setText(tbDanhSachKhachHang.getValueAt(dong,null).toString());
            NguoiDung nguoiDung = new NguoiDung();
            nguoiDung = nguoiDungList.get(dong);
            txtID.setText(String.valueOf(nguoiDung.getId()));
            txtCMND.setText(nguoiDung.getCmnd());
            txtDienThoai.setText(nguoiDung.getDienThoai());
            txtDiaChi.setText(nguoiDung.getDiaChi());
            txtTenKhachHang.setText(nguoiDung.getTenNguoiDung());
            dtNgaySinh.setDate(nguoiDung.getNgaySinh());
        }
    }//GEN-LAST:event_tbDanhSachKhachHangMouseClicked

    private void btnCapNhatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCapNhatActionPerformed
        // TODO add your handling code here:
        int dong = tbDanhSachKhachHang.getSelectedRow();
        if(dong >= 0){
            int id = Integer.parseInt(txtID.getText().toString());
            NguoiDung nd = NguoiDungDAO.LayKhachHangByID(id);
            if(nd != null){
                nd.setCmnd(txtCMND.getText().toString());
                nd.setDienThoai(txtDienThoai.getText().toString());
                nd.setDiaChi(txtDiaChi.getText().toString());
                nd.setTenNguoiDung(txtTenKhachHang.getText().toString());
                nd.setNgaySinh(dtNgaySinh.getDate());
                nd.setIsDelete(false);
                boolean kq = NguoiDungDAO.CapNhatKhachHang(nd);
                if(kq == true){
                    try {
                        AutoLoad();
                        JOptionPane.showMessageDialog(this, "Cập nhật thành công");
                    } catch (Exception ex) {
                        Logger.getLogger(DanhSachKhachHangGUI.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        }
        else{
            JOptionPane.showMessageDialog(this, "Vui lòng chọn dữ liệu trên lưới");
        }
    }//GEN-LAST:event_btnCapNhatActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCapNhat;
    private javax.swing.JButton btnThem;
    private javax.swing.JButton btnXoa;
    private org.jdesktop.swingx.JXDatePicker dtNgaySinh;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tbDanhSachKhachHang;
    private javax.swing.JTextField txtCMND;
    private javax.swing.JTextField txtDiaChi;
    private javax.swing.JTextField txtDienThoai;
    private javax.swing.JTextField txtID;
    private javax.swing.JTextField txtTenKhachHang;
    // End of variables declaration//GEN-END:variables
    }

